<?php
require_once (dirname(__FILE__) . '/../phptal/PHPTAL.php');
require_once (dirname(__FILE__) . '/converter.php');
require_once (dirname(__FILE__) . '/canvasPixelSizeCalculator.php');
require_once (dirname(__FILE__) . '/workspaceCanvasSizeCalculator.php');
	
function buildTemplate($givenCanvasConfigs, $templateFilePath){
	$template = new PHPTAL($templateFilePath);

	$canvasConfigurations = array();
	foreach($givenCanvasConfigs as $givenCanvasConfig){
		//given
		$input = $givenCanvasConfig;
		$dpi = $input->dpi;
		$price = $input->price;
		
		$givenExtendedCanvasWidthCm = $input->extendedCanvasWidthCm;
		$givenExtendedCanvasHeightCm = $input->extendedCanvasHeightCm;
		$givenBleedXLCm = $input->bleedXLCm;
		$givenBleedXRCm = $input->bleedXRCm;
		$givenBleedYTCm = $input->bleedYTCm;
		$givenBleedYBCm = $input->bleedYBCm;
		
		$extendedCanvasWidthInch = cmToInch($givenExtendedCanvasWidthCm);
		$extendedCanvasHeightInch = cmToInch($givenExtendedCanvasHeightCm);
		$bleedXLInch = cmToInch($givenBleedXLCm);
		$bleedXRInch = cmToInch($givenBleedXRCm);
		$bleedYTInch = cmToInch($givenBleedYTCm);
		$bleedYBInch = cmToInch($givenBleedYBCm);
		
		$canvasWidth = $extendedCanvasWidthInch * $dpi;
		$canvasHeight = $extendedCanvasHeightInch * $dpi;
		$bleedXLPixels = $bleedXLInch * $dpi;
		$bleedXRPixels = $bleedXRInch * $dpi;
		$bleedYTPixels = $bleedYTInch * $dpi;
		$bleedYBPixels = $bleedYBInch * $dpi;
		
		
		//adapted to the workspace
		$extendedCanvasWidthPixels = $bleedXLPixels + $canvasWidth + $bleedXRPixels;
		$extendedCanvasHeightPixels = $bleedYTPixels + $canvasHeight + $bleedYBPixels;
		
		$scale = calculateWorkspaceCanvasSize($extendedCanvasWidthPixels, $extendedCanvasHeightPixels, WORKSPACE_WIDTH * ACTIVE_WORKSPACE_FACTOR, WORKSPACE_HEIGHT * ACTIVE_WORKSPACE_FACTOR);
		$scaleX = $scale[0];
		$scaleY = $scale[1];
		$minimumScale = min($scaleX, $scaleY);
		
		$scaledExtendedCanvasWidth = $extendedCanvasWidthPixels * $minimumScale;
		$scaledExtendedCanvasHeight = $extendedCanvasHeightPixels * $minimumScale;
		$bleedXLPixels = $bleedXLPixels * $minimumScale;
		$bleedXRPixels = $bleedXRPixels * $minimumScale;
		$bleedYTPixels = $bleedYTPixels * $minimumScale;
		$bleedYBPixels = $bleedYBPixels * $minimumScale;
		
		/*$scaledExtendedCanvasWidth = $extendedCanvasWidthPixels;
		$scaledExtendedCanvasHeight = $extendedCanvasHeightPixels;
		$bleedXLPixels = $bleedXLPixels;
		$bleedXRPixels = $bleedXRPixels;
		$bleedYTPixels = $bleedYTPixels;
		$bleedYBPixels = $bleedYBPixels;
		$minimumScale = 1;*/
		
		$containerWidthInch = $extendedCanvasWidthInch + $bleedXLInch + $bleedXRInch;
		$containerHeightInch = $extendedCanvasHeightInch + $bleedYTInch + $bleedYBInch;
		
		array_push($canvasConfigurations, new CanvasConfiguration("$givenExtendedCanvasWidthCm" . "cm x " . "$givenExtendedCanvasHeightCm" . "cm [$price]" , 
			"/viae/canvas.php?extendedCanvasWidth=$scaledExtendedCanvasWidth&extendedCanvasHeight=$scaledExtendedCanvasHeight&bleedXL=$bleedXLPixels&bleedXR=$bleedXRPixels&bleedYT=$bleedYTPixels&bleedYB=$bleedYBPixels&scale=$minimumScale&containerWidthInch=$containerWidthInch&containerHeightInch=$containerHeightInch&dpi=$dpi&price=$price"));
	}
	
	
	/*array_push($canvasConfigurations, new CanvasConfiguration("$extendedCanvasWidthCm" . "cm x " . "$extendedCanvasHeightCm" . "cm" , "/canvas.php?extendedCanvasWidth=$extendedCanvasWidth&extendedCanvasHeight=$extendedCanvasHeight&bleedX=$bleedX&bleedY=$bleedY"));
	array_push($canvasConfigurations, new CanvasConfiguration("800x400", "/canvas.php?extendedCanvasWidth=800&extendedCanvasHeight=400&bleedX=100&bleedY=50"));
	array_push($canvasConfigurations, new CanvasConfiguration("500x200", "/canvas.php?extendedCanvasWidth=500&extendedCanvasHeight=200&bleedX=100&bleedY=10"));*/
	
	$template->canvasConfigurations = $canvasConfigurations;
	$template->cmWidth = $givenCanvasConfigs[0]->extendedCanvasWidthCm;
	$template->cmHeight = $givenCanvasConfigs[0]->extendedCanvasHeightCm;
	$template->workspaceDialogHeight = WORKSPACE_DIALOG_HEIGHT;
	$template->workspaceDialogWidth = WORKSPACE_DIALOG_WIDTH;
	$template->imageUploadDialogWidth = IMAGE_UPLOAD_DIALOG_WIDTH;

	return $template;
}
	
class CanvasConfiguration{
	public $name;
	public $url;
	public function __construct($name, $url){
		$this->name = $name;
		$this->url = $url;
	}
}

class GivenCanvasConfiguration{
	public $extendedCanvasWidthCm;
	public $extendedCanvasHeightCm;
	public $bleedXLCm;
	public $bleedXRCm;
	public $bleedYTCm;
	public $bleedYBCm;
	public $dpi;
	public $price;
	
	public function __construct(){
	}
}

?>